package com.eclab.controller;

import com.eclab.entity.Account;
import com.eclab.exception.BizException;
import com.eclab.service.AccountService;
import com.eclab.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Account)表控制层
 *
 * @author marin
 * @since 2020-04-23 09:46:33
 */

@Slf4j
@Controller
@RequestMapping("/account")
@Api(value = "账户信息",tags = {"账户信息接口"})
public class AccountController {
    /**
     * 服务对象
     */
    @Resource
    private AccountService accountService;

    private static Result result = new Result();

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询用户")
    @RequestMapping(value = "/findOne",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Account findOne(Integer id) {
        Account account = accountService.queryById(id);
        log.info("查询成功！");
        result.clean();
        result.setMessage("查询成功！");
        result.setResult(account);
        if(id == null){
        throw new BizException("-1","姓名不能为空");
        }
        log.info(String.valueOf(result));
        return account;
    }

    @ApiOperation(value = "查询所有用户")
    @RequestMapping(value = "/findAllAccount",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public List<Account> findAllAccount(Integer offset,Integer limit) {
        List<Account> accounts = accountService.queryAllByLimit(offset, limit);
        return accounts;
    }

    @ApiOperation(value = "保存用户")
    @RequestMapping(value = "/saveAccount",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Account saveAccount(Account account) {
        Account accounts = accountService.insert(account);
        System.out.println(accounts.getUsername());
        return accounts;
    }

    @ApiOperation(value = "更新用户")
    @RequestMapping(value = "/updateAccount",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Account UpdateAccount(Account account) {
        Account accounts = accountService.update(account);
        return accounts;
    }

    @ApiOperation(value = "删除用户")
    @RequestMapping(value = "/deleteAccount",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public String deleteAccount(Integer id) {
        if(id == null){
            throw new BizException("-1","姓名不能为空");
        }
        String result = accountService.deleteById(id);
        return result;
    }

    @ApiOperation(value = "用户转账")
    @RequestMapping(value = "/transform",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public String transformAccount(String source,String target,Float money) {
        if(source == null || target == null || money == null){
            throw new BizException("-1","参数不能为空");
        }
        String result = accountService.transform(source,target,money);
        return result;
    }

    @ApiOperation(value = "Result包装查询返回的所有用户")
    @RequestMapping(value = "/findAll",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Result findAll() {
        List<Account> accounts = accountService.findAll();
        result.clean();
        result.setResult(accounts);
        result.setMessage("查询成功！");
        result.setCode("200");
        return result;
    }

    @ApiOperation(value = "Map包装查询返回的所有用户")
    @RequestMapping(value = "/findAllAcc",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Map<String,Object> findAllAcc() {
        Map<String,Object> maps = new HashMap<>();
        List<Account> accounts = accountService.findAll();
        maps.put("accounts",accounts);
        maps.put("user","hello");
        maps.put("hello","world");
       /* result.clean();
        result.setResult(accounts);
        result.setMessage("查询成功！");
        result.setCode("200");*/
        return maps;
    }

    /**
     * @description:
     * @param null
     * @return:
     * @author: Marin
     * @time: 2020/4/30 9:02
     */     
    public Account getAllAccount(){
        Account account = new Account();
        return account;
    }

}